//
//  SetPasswordViewController.m
//  WuJieLive
//
//  Created by iMac on 2017/10/19.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "SetPasswordViewController.h"
#import "ReferralViewController.h"
#import "UserProtocolViewController.h"

@interface SetPasswordViewController ()
@property (weak, nonatomic) IBOutlet UIButton *protocolBtn;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UIButton *doneBtn;
@property (weak, nonatomic) IBOutlet UITextField *referralText;


@end

@implementation SetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)setNavi
{
    self.title = @"注册";
}


- (void)setUI
{
    NSString * tempString = @"注册即代表同意《用户协议》";
    NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:tempString];
    [self.protocolBtn setAttributedTitle:string forState:(UIControlStateNormal)];
    NSRange range = [tempString rangeOfString:@"《用户协议》"];
    [string setAttributes:@{NSForegroundColorAttributeName:Color(51, 51, 51)} range:range];
    NSRange tempRange = [tempString rangeOfString:@"注册即代表同意"];
    [string setAttributes:@{NSForegroundColorAttributeName:TextLessImportColor} range:tempRange];
    [self.passwordText addTarget:self action:@selector(textChange:) forControlEvents:(UIControlEventEditingChanged)];
}

- (void)textChange:(UITextField *)textField
{
    if (_passwordText.text.length >= 6) {
        self.doneBtn.userInteractionEnabled = YES;
        [self.doneBtn setBackgroundColor:MainColor];
    }else
    {
        [self.doneBtn setBackgroundColor:MainBtnColor];
        self.doneBtn.userInteractionEnabled = NO;
    }
}

- (IBAction)doneAction:(id)sender {
    
    if (self.referralText.text.length == 11) {  // 如果有推荐人
        [MBProgressHUD showMessage:@"正在注册" toView:self.view];
        NSDictionary * dict = @{@"phone_redis_key":self.redisKey,@"password":self.passwordText.text,@"reference":self.referralText.text};
        
        [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/register-password",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {  // 注册接口
            [MBProgressHUD hideHUDForView:self.view];
            if (isSuccess) {
                self.password = self.passwordText.text;
                if (isSuccess) {
                    ReferralViewController * vc = [[ReferralViewController alloc]init];
                    vc.nickNameString = [[responseObject objectForKey:@"data"] objectForKey:@"reference_name"];
                    vc.telePhoneString = [[responseObject objectForKey:@"data"] objectForKey:@"reference_phone"];
                    vc.headerImageString = [[responseObject objectForKey:@"data"] objectForKey:@"reference_portrait"];
                    vc.popType = 0;
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
        }];
    }else if (self.referralText.text.length > 0 && self.referralText.text.length != 11)
    {
        [MBProgressHUD showError:@"请检查推荐人的电话号码"];
    }
    else
    {
        [MBProgressHUD showMessage:@"正在注册" toView:self.view];  // 没有推荐人
        NSDictionary * dict = @{@"phone_redis_key":self.redisKey,@"password":self.passwordText.text};
        [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/register",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
            if (isSuccess) {
                self.password = self.passwordText.text;
                NSDictionary * dic = @{@"phone_mob":self.telephoneNumber,@"password":self.password};
                [HTTPTool POSTForm:[NSString stringWithFormat:@"%@member/login",BaseUrl] para:dic success:^(BOOL isSuccess, id responseObject) {
                    [MBProgressHUD hideHUDForView:self.view];
                    if (isSuccess) {
                        self.isLogin = YES;
                        self.userToken = responseObject [@"data"][@"token"];
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }
                } failure:^(NSError *error) {
                    [MBProgressHUD hideHUDForView:self.view];
                }];
            }
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
        }];
    }
}


- (IBAction)protocolAction:(UIButton *)sender {
    
    UserProtocolViewController * vc = [[UserProtocolViewController alloc]initWithNibName:@"UserProtocolViewController" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (IBAction)safePasswordAction:(UIButton *)sender {
    
    sender.selected = !sender.isSelected;
    self.passwordText.secureTextEntry = !sender.selected;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
